package com.sxm.yiwei.activitis;

import android.app.Activity;


import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import com.sxm.yiwei.R;
import com.sxm.yiwei.fragments.LinkAddressFragment;
import com.sxm.yiwei.fragments.LinkReciverFragment;

public class AddCloseActivity extends BaseActivity implements View.OnClickListener{

    private Button bt1;
    private Button bt2;
    private LinkAddressFragment linkAddressFragment;
    private LinkReciverFragment linkReciverFragment;
    private ImageView likback;
    private Button addlink;
    private String is;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_close);
        bt1 = (Button) findViewById(R.id.bt1);
        bt2 = (Button) findViewById(R.id.bt2);
        likback = (ImageView) findViewById(R.id.mine_link_back);
        addlink = (Button) findViewById(R.id.mine_link_addbt);
        is="0";
        bt1.setSelected(true);
        bt1.setOnClickListener(this);
        bt2.setOnClickListener(this);
        likback.setOnClickListener(this);
        addlink.setOnClickListener(this);
        onFrgamentSelect(1);
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        //super.onSaveInstanceState(outState);
    }

    private void onFrgamentSelect(int i) {
        FragmentManager fragmentManager = getFragmentManager();
        android.app.FragmentTransaction ft = fragmentManager.beginTransaction();
        onHideFragment(ft);
        switch (i){
            case 1:
                if (linkAddressFragment==null){
                    linkAddressFragment = new LinkAddressFragment();
                    ft.add(R.id.fg,linkAddressFragment);
                }else {
                    ft.show(linkAddressFragment);
                }
                break;
            case 2:
                if (linkReciverFragment==null){
                    linkReciverFragment = new LinkReciverFragment();
                    ft.add(R.id.fg,linkReciverFragment);
                }else {
                    ft.show(linkReciverFragment);
                }
                break;
        }
        ft.commit();
    }

    private void onHideFragment(FragmentTransaction ft) {
        if (linkAddressFragment!=null){
            ft.hide(linkAddressFragment);
        }
        if (linkReciverFragment!=null) {
            ft.hide(linkReciverFragment);
        }

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.bt1:
                is="0";
                bt1.setSelected(true);
                bt2.setSelected(false);
               onFrgamentSelect(1);
                break;
            case R.id.bt2:
                is= "1";
                bt1.setSelected(false);
                bt2.setSelected(true);
               onFrgamentSelect(2);
                break;
            case R.id.mine_link_back:
                finish();
                break;
            case R.id.mine_link_addbt:
                Intent intent = new Intent(AddCloseActivity.this,AddLinkmanActivity.class);

                intent.putExtra("is", is);
                startActivity(intent);
                break;
        }
    }
}
